package com.coco.common.mqlistener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author:fei
 * @Date:2021/7/16
 */
@Component
@RabbitListener(queues = "email")
@Slf4j
public class EmailListener {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    @RabbitHandler
    public void sendEmail(String email){
        log.info("给"+email+"发送邮件");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("coco直播注册通知");
        simpleMailMessage.setText("亲爱的"+email+"用户您与"+format+"成功注册了coco直播平台，快去使用吧！！！");
        simpleMailMessage.setFrom("1197941802@qq.com");
        simpleMailMessage.setSentDate(new Date());
        simpleMailMessage.setTo(email);
        javaMailSender.send(simpleMailMessage);
    }
}
